/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.iterators.ArrayIterator;
import persistence.rb_raw_materials_persist;
import vo.rb_purchase;

import vo.rb_raw_mat_stock;
import vo.rb_raw_materials;

/**
 *
 * @author Subin
 */
public class RawMaterialController {
    
    public static RawMaterialController getInstance() {
        
        return new RawMaterialController();
    }
    

    public boolean create(rb_raw_materials newRawMat){
            return new rb_raw_materials_persist().getInstance().insert(newRawMat);
        
    }
    
    public rb_raw_materials update(rb_raw_materials rawMat){
        
        return null;
    }
    
    public rb_raw_materials[] fetchAll(){
        
        return null;
    }
    
    public String delete(rb_raw_materials rawMat){
        
        return null;
    }
    public rb_raw_materials fetch(String rawMatId){
        
        return null;
    }
    
    
    
    public List updateRawStock(rb_purchase[] purchaseArray){
        List currentStockStatus = new ArrayList();
        Iterator iter = new ArrayIterator(purchaseArray);
        while (iter.hasNext()){
            rb_purchase current = (rb_purchase) iter.next();
            rb_raw_mat_stock curRawMatStock = new rb_raw_mat_stock();
            curRawMatStock.setRaw_mat_id(current.getRaw_mat_id());
            curRawMatStock.setCurrent_unit_price(current.getUnit_cost());
            curRawMatStock.setLast_updated_date(current.getDate());
            curRawMatStock.setLast_updated_invoice_no(current.getPurchase_invoice_no());
            rb_raw_mat_stock rawMatStock = fetchStock(current.getRaw_mat_id());
            if(rawMatStock!=null){
                int currentStock = rawMatStock.getCurrent_stock()+current.getQuantity();
                curRawMatStock.setCurrent_stock(currentStock);
                curRawMatStock.setCurrent_value(currentStock*current.getUnit_cost());
                updateStock(curRawMatStock);
            }
            else{
                curRawMatStock.setCurrent_stock(current.getQuantity());
                curRawMatStock.setCurrent_value(current.getUnit_cost()*current.getUnit_cost());
                insertStock(curRawMatStock);
            }
            currentStockStatus.add(curRawMatStock);
        }
        return currentStockStatus;
    }
    
    public rb_raw_mat_stock fetchStock(String rawMatId){
        
        return new rb_raw_mat_stock();
    }
    
    public rb_raw_mat_stock updateStock(rb_raw_mat_stock rawMatStock){
        
        return null;
    }
    
    public rb_raw_mat_stock insertStock(rb_raw_mat_stock rawMatStock){
        
        return null;
    } 
   
    public Map<String,String> fetchAllRawMaterialIdAndNames() {
        
        Map<String,String> map = new HashMap<String, String>();
         List<rb_raw_materials> mat=rb_raw_materials_persist.getInstance().fetch();
        Iterator it=mat.iterator();
        while(it.hasNext()) {
        
            rb_raw_materials man_names=(rb_raw_materials)it.next();
            map.put(man_names.getRaw_mat_name(), man_names.getRaw_mat_id());
            }
        return map;
                
    }
    public String[] fetchAllRawMaterialNames() {
        List<rb_raw_materials> mat=rb_raw_materials_persist.getInstance().fetch();
        String[] raw_names=new String[mat.size()];
        Iterator it=mat.iterator();
        int cnt=0;
        while(it.hasNext()) {
            rb_raw_materials man_names=(rb_raw_materials)it.next();
            raw_names[cnt]=man_names.getRaw_mat_name();
            cnt++;
         }
        return raw_names;
    }
}
